angular.module('tab-orders', ['swiper-x', 'slide-banner', 'img-gallery', 'data-srv'])

.controller 'TabOrdersCtrl', ($localStorage,$scope, $state, $http, $window, $ionicHistory, $ionicPopover, $ionicPopup, orderInfo,showToast, paginationFactory,userData, $log,$ionicScrollDelegate) ->
    logger = $log.getInstance 'TabOrdersCtrl'
    logger.info 'load TabOrdersCtrl'
    $scope.$storage = $localStorage
    paginationSrv = new paginationFactory('TabOrdersCtrl','/tradeapi/')
    paginationSrv.reload(true, {
        action:'orderslist'
        userid: userData.getToken()
        stype: ''
    })
    unWatch = paginationSrv.watch (orderData) ->
        $scope.$storage.orders = orderData


    getOrderDetail = ->
        $http.get('/tradeapi/',params:{action:'ordersinfo',userid:userData.getToken(),id:$localStorage['oDOrderInfo'].id}) #userData.getToken()
        .success (resp) ->
            console.log resp
            if resp?.status == '0'
                $localStorage['odOrderDetail']=resp
                console.log $localStorage['odOrderDetail']
                #$scope.orderDetail.isupk='1' #测试用
                getWuliu()
            else
                angular.noop()
#todo 错误处理
        .error (err) ->
#todo 错误处理
            console.log err
    getWuliu = ->
        if $localStorage['odOrderDetail'].sendcode != ''
            $http.get("http://api.kuaidi100.com/api?&id=cfe1e9114f773ce2",params:{com:$localStorage['odOrderDetail'].sendcode,nu:$localStorage['odOrderDetail'].sendno,show:0,order:'desc'})
            .success (resp) ->
                if resp?.status == '1'
                    $localStorage['wuliuData'] = resp.data
                    $localStorage['wuliuDataState'] = resp.state
                else
                    if resp.status == '0'
                        $localStorage['wuliuData']=[{context:resp.message}]
#                        showToast resp.message
                    else if resp.status == '2'
                        $localStorage['wuliuData']=[{context:resp.message}]
#                        showToast resp.message
            .error ->
                showToast '网络异常'
#todo 错误处理
        else
            return false
    _.extend $scope,
        searchData:
            words:undefined
        doSearch:()->
            paginationSrv.reload(true, {
                action:'orderslist'
                userid: userData.getToken()
                tradeno:$scope.searchData.words
                stype: $scope.orderStatus[orderInfo.orderStatusIndex].key
            })
        clear: ->
            $scope.searchData.words = undefined
            $scope.doSearch()
        canLoadMore: -> paginationSrv.canLoadMore()
        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'
        goYPK: ->
            $state.go 'tab.market-orders'
        goodsInClass: (i)->
            orderInfo.orderStatusIndex = i
            paginationSrv.reload(true, {
                action:'orderslist'
                userid: userData.getToken()
                stype: $scope.orderStatus[i].key
            })
        goSend: (data, event,i)->
            event.stopPropagation()
            console.log 'send'
            $http.post('/orders/', {
                action: 'changestatus',
                userid: userData.getToken(),
                id: data.id
                status: 2
            })
            .success (resp) ->
                if resp?.status == '0'
                    showToast '已发货'
                    $scope.orders.splice(i,1)
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
        goOrderDetail: (data)->
            orderInfo.order = data
            $localStorage['oDOrderInfo'] = data
            $localStorage['odOrderDetail'] = undefined
            getOrderDetail()
            $state.go 'tab.order-detail'

    $scope.orderStatus = [
        {key: '', name: '全部订单', func: $scope.goodsInClass}
        {key: '1', name: '线上支付', func: $scope.goodsInClass}
        {key: '2', name: '银行转账', func: $scope.goodsInClass}
        {key: '3', name: '货到付款', func: $scope.goodsInClass}
    ]
    $scope.$on '$destory', ->
        logger.log 'TabOrdersCtrl $destory'
        unWatch()
#    $scope.$on '$stateChangeSuccess', ->
#        if $state.is 'tab.orders'
#            paginationSrv.reload(true, {
#                action:'orderslist'
#                userid: userData.getToken() #userData.getToken()
#                stype: $scope.orderStatus[orderInfo.orderStatusIndex].key
#            })
#            $ionicScrollDelegate.scrollTop()
